package org.oslims.client.presenter;

import org.oslims.client.MainEventBus;
import org.oslims.client.UserUtil;
import org.oslims.client.service.UserServiceAsync;
import org.oslims.client.view.impl.RootViewImpl;
import org.oslims.domain.BaseUser;
import org.oslims.domain.Operator;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.mvp4g.client.annotation.InjectService;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;

@Presenter(view = RootViewImpl.class)
public class RootPresenter extends BasePresenter<RootViewImpl, MainEventBus> {

	private UserServiceAsync userService;

	@InjectService
	public void setUserService(UserServiceAsync userService) {
		this.userService = userService;
	}

	public void bind() {
		view.getUserManagementLink().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent arg0) {
				eventBus.showUserManagement(view.getMainWidget());
			}
		});

		view.getDepartmentManagementLink().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent arg0) {
				eventBus.showDepartmentManagement(view.getMainWidget());
			}
		});

		view.getParameterManagementLink().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent arg0) {
				eventBus.showParameterManagement(view.getMainWidget());
			}
		});

		view.getAnalysisManagementLink().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent arg0) {
				eventBus.showAnalysisManagement(view.getMainWidget());
			}
		});

		view.getSampleTypeManagementLink().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent arg0) {
				eventBus.showSampleTypeManagement(view.getMainWidget());
			}
		});

		view.getRequestManagementLink().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent arg0) {
				eventBus.showRequestManagement(view.getMainWidget());
			}
		});

		view.getSampleManagementLink().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				eventBus.showRootSampleManagement(view.getMainWidget());
			}
		});

		view.getAnalysisPlaningLink().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent arg0) {
				eventBus.showAnalysisPlaning(view.getMainWidget());
			}
		});

		view.getEnterResultsLink().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent arg0) {
				eventBus.showEnterResultsByAnalysis(view.getMainWidget());
			}
		});

		view.getLabValidateResultsLink().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent arg0) {
				eventBus.showLabValidateResults(view.getMainWidget());
			}
		});

		view.getScientValidateResultsLink().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent arg0) {
				eventBus.showScientValidateResults(view.getMainWidget());
			}
		});

		view.getShowResultsLink().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent arg0) {
				eventBus.showResults(view.getMainWidget());
			}
		});
	}

	public void onStart() {
		// eventBus.showUserManagement(view.getMainWidget());
		userService.findByEMailAndPassword("peter@pratscher.org", "rost17", new AsyncCallback<BaseUser>() {

			@Override
			public void onSuccess(BaseUser result) {
				UserUtil.setLoggedInUser(result);

				Label l = new Label();
				l.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
				l.setText("Logged in User: " + UserUtil.getLoggedInUser().toString());
				view.getMainWidget().addNorth(l, 1.5d);
				eventBus.showRequestManagement(view.getMainWidget());
			}

			@Override
			public void onFailure(Throwable error) {
				eventBus.error(error.getMessage());
			}
		});

		// Get a test uses entering parameter results
		userService.findByEMailAndPassword("test@operator.com", "rost17", new AsyncCallback<BaseUser>() {

			@Override
			public void onSuccess(BaseUser result) {
				UserUtil.setTestOperator((Operator) result);
			}

			@Override
			public void onFailure(Throwable error) {
				eventBus.error(error.getMessage());
			}
		});
	}
}
